<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yang-da-xiong.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="前言记录opencv有关像素操作的一些函数和功能">
<meta property="og:type" content="article">
<meta property="og:title" content="opencv学习笔记之像素处理">
<meta property="og:url" content="https://yang-da-xiong.gitee.io/2022/02/12/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%83%8F%E7%B4%A0%E5%A4%84%E7%90%86/index.html">
<meta property="og:site_name" content="杨大熊的代码世界">
<meta property="og:description" content="前言记录opencv有关像素操作的一些函数和功能">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/5c45d97d014d4cad8b8da2c2bc999a9d.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/303e04d8824b4dcabf61e919c4276451.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/93afcc9d87274518800451d1eca9a2b3.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ea21027dee564caba5efc9ecb0c65416.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/55372a253b184435bb7b89baa070d5f6.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ada0c8e325934ea8a69cc0e5e7b89178.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/126c428ecab9450287962e030ea3e685.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="article:published_time" content="2022-02-12T04:17:26.267Z">
<meta property="article:modified_time" content="2022-02-14T08:24:19.261Z">
<meta property="article:author" content="杨大熊">
<meta property="article:tag" content="c++">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/5c45d97d014d4cad8b8da2c2bc999a9d.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">

<link rel="canonical" href="https://yang-da-xiong.gitee.io/2022/02/12/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%83%8F%E7%B4%A0%E5%A4%84%E7%90%86/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>opencv学习笔记之像素处理 | 杨大熊的代码世界</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">杨大熊的代码世界</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://yang-da-xiong.gitee.io/2022/02/12/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%83%8F%E7%B4%A0%E5%A4%84%E7%90%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="杨大熊">
      <meta itemprop="description" content="各类IT知识分享">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="杨大熊的代码世界">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          opencv学习笔记之像素处理
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-02-12 12:17:26" itemprop="dateCreated datePublished" datetime="2022-02-12T12:17:26+08:00">2022-02-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-02-14 16:24:19" itemprop="dateModified" datetime="2022-02-14T16:24:19+08:00">2022-02-14</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>记录opencv有关像素操作的一些函数和功能</p>
<span id="more"></span>

<h1 id="一、多通道的分离合并"><a href="#一、多通道的分离合并" class="headerlink" title="一、多通道的分离合并"></a>一、多通道的分离合并</h1><h4 id="多通道分离函数split（）"><a href="#多通道分离函数split（）" class="headerlink" title="多通道分离函数split（）"></a>多通道分离函数split（）</h4><p>形式1：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void split(const Mat &amp; scr,Mat * mvbegin)</span><br></pre></td></tr></table></figure>

<p>scr：待分离的多通道图像</p>
<p>mvbegin：分离后的单通道图像，为数组形式，数组大小需要与图像的通道数相同</p>
<p>形式2：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void split(InputArray m,OutputArrayOfArrays mv)</span><br></pre></td></tr></table></figure>

<p>m：待分离的多通道图像</p>
<p>mv：分离后的单通道图像，为向量（vector）形式</p>
<p>对比：形式1的第二个参数输出一个Mat型的数组，并且数组长度要与图像的通道数相同并且提前定义。</p>
<p>形式2的第二个参数输出一个vector&lt;Mat&gt;容器，不需要知道多通道图像的通道数。</p>
<h4 id="多通道合并函数merge（）"><a href="#多通道合并函数merge（）" class="headerlink" title="多通道合并函数merge（）"></a>多通道合并函数merge（）</h4><p>形式1：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void merge(const Mat * mv,size_t count,OutputArray dst)</span><br></pre></td></tr></table></figure>

<p>mv（第一种重载原型参数）：需要合并的图像数组，其中每个图像必须拥有相同的尺寸和数据类型</p>
<p>count：输入的图像数组的长度，其数值必须大于0</p>
<p>dst：合并输出后的图像，与mv[0]具有相同的尺寸和数据类型，通道数等于所有输入图像的通道数总和</p>
<p>形式2：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void merge(InputArrayOfArrays mv,OutputArray dst)</span><br></pre></td></tr></table></figure>

<p>mv（第二种重载原型参数）：需要合并的图像向量（vector），其中每个图像必须拥有相同的尺寸和数据类型</p>
<p>dst：合并输出后的图像，与mv[0]具有相同的尺寸和数据类型，通道数等于所有输入图像的通道数总和</p>
<p>示例程序：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;opencv2\opencv.hpp&gt;</span>  <span class="comment">//加载OpenCV4的头文件</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> cv; <span class="comment">//OpenCV命名空间</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Mat img = imread(<span class="string">&quot;lena.jpg&quot;</span>);</span><br><span class="line">	<span class="keyword">if</span> (img.empty())</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;请确认图像文件名称是否正确&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	Mat HSV;</span><br><span class="line">	cvtColor(img, HSV, COLOR_RGB2HSV); <span class="comment">//RGB图像模型转HSV</span></span><br><span class="line">	Mat imgs0, imgs1, imgs2;  <span class="comment">//用于存放数组类型的结果</span></span><br><span class="line">	Mat imgv0, imgv1, imgv2;  <span class="comment">//用于存放vector类型的结果</span></span><br><span class="line">	Mat result0, result1, result2;  <span class="comment">//多通道合并的结果</span></span><br><span class="line">	Mat imgs[<span class="number">3</span>];</span><br><span class="line">	split(img, imgs);  <span class="comment">//将img图像的3个通道分别存放到imgs数组中</span></span><br><span class="line">	imgs0 = imgs[<span class="number">0</span>];</span><br><span class="line">	imgs1 = imgs[<span class="number">1</span>];</span><br><span class="line">	imgs2 = imgs[<span class="number">2</span>];</span><br><span class="line">	imshow(<span class="string">&quot;RGB-B通道&quot;</span>, imgs0);  <span class="comment">//显示分离后B通道的像素值</span></span><br><span class="line">	imshow(<span class="string">&quot;RGB-G通道&quot;</span>, imgs1);  <span class="comment">//显示分离后G通道的像素值</span></span><br><span class="line">	imshow(<span class="string">&quot;RGB-R通道&quot;</span>, imgs2);  <span class="comment">//显示分离后R通道的像素值</span></span><br><span class="line">	imgs[<span class="number">2</span>] = img;  <span class="comment">//将R通道变成完整的RGB三通道模式，是的数组中图像的通道数不一致</span></span><br><span class="line">	merge(imgs, <span class="number">3</span>, result0);  <span class="comment">//合并图像</span></span><br><span class="line">	<span class="comment">//imshow(&quot;result0&quot;,result0);  //imshow最多显示4个通道，因此结果在Image Watch中查看</span></span><br><span class="line">	Mat zero = zero.zeros(img.rows, img.cols, CV_8UC1);  <span class="comment">//构建一个全为0的矩阵</span></span><br><span class="line">	imgs[<span class="number">0</span>] = zero;</span><br><span class="line">	imgs[<span class="number">2</span>] = zero;</span><br><span class="line">	merge(imgs, <span class="number">3</span>, result1);  <span class="comment">//用于还原G通道的真实情况，合并结果为绿色</span></span><br><span class="line">	imshow(<span class="string">&quot;result1&quot;</span>, result1);  <span class="comment">//显示合并结果</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//输入vector参数的多通道分离与合并</span></span><br><span class="line">	<span class="built_in">vector</span>&lt;Mat&gt; imgv;</span><br><span class="line">	split(HSV, imgv);  <span class="comment">//第二个参数为vector</span></span><br><span class="line">	imgv0 = imgv.at(<span class="number">0</span>);</span><br><span class="line">	imgv1 = imgv.at(<span class="number">1</span>);</span><br><span class="line">	imgv2 = imgv.at(<span class="number">2</span>);</span><br><span class="line">	imshow(<span class="string">&quot;HSV-H通道&quot;</span>, imgv0);  <span class="comment">//显示分离后H(色度)通道的像素值</span></span><br><span class="line">	imshow(<span class="string">&quot;HSV-S通道&quot;</span>, imgv1);  <span class="comment">//显示分离后S(饱和度)通道的像素值</span></span><br><span class="line">	imshow(<span class="string">&quot;HSV-V通道&quot;</span>, imgv2);  <span class="comment">//显示分离后V(亮度)通道的像素值</span></span><br><span class="line">	imgv.push_back(HSV);  <span class="comment">//将vector中的图像通道数变成不一致</span></span><br><span class="line">	merge(imgv, result2);  <span class="comment">//合并图像</span></span><br><span class="line">	<span class="comment">//imshow(&quot;result2&quot;,result2);  //imshow最多显示4个通道，因此结果在Image Watch中查看</span></span><br><span class="line">	waitKey(<span class="number">0</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;  <span class="comment">//程序结束</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>imgs0（B通道）：</p>
<img src="https://img-blog.csdnimg.cn/5c45d97d014d4cad8b8da2c2bc999a9d.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="40%">

<p>imgs1（G通道）：</p>
<img src="https://img-blog.csdnimg.cn/303e04d8824b4dcabf61e919c4276451.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="40%">

<p>imgs2（R通道）：</p>
<img src="https://img-blog.csdnimg.cn/93afcc9d87274518800451d1eca9a2b3.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="40%">

<p>result0（总共5个通道）:</p>
<img src="https://img-blog.csdnimg.cn/ea21027dee564caba5efc9ecb0c65416.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="40%">

<p>看起来还有点酷</p>
<p>result1（只显示绿色G通道）：</p>
<img src="https://img-blog.csdnimg.cn/55372a253b184435bb7b89baa070d5f6.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="40%">

<p>result2（HSV模型下的5通道图像）：</p>
<img src="https://img-blog.csdnimg.cn/ada0c8e325934ea8a69cc0e5e7b89178.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="40%">



<h1 id="二、图像像素统计"><a href="#二、图像像素统计" class="headerlink" title="二、图像像素统计"></a>二、图像像素统计</h1><h4 id="计算图像的平均值和标准差"><a href="#计算图像的平均值和标准差" class="headerlink" title="计算图像的平均值和标准差"></a>计算图像的平均值和标准差</h4><p>形式1：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mean(InputArray src,InputArray mask=noArray())</span><br></pre></td></tr></table></figure>

<p>src：待求平均值的图像矩阵</p>
<p>mask：用于标记求取哪些区域的平均值</p>
<p>可以求取的图像通道数为1~4，该函数<strong>返回</strong>一个Scalar类型的变量，通过Scalar[n]来查看第n个通道的平均值。</p>
<p>形式2：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">meanStdDev(InputArray src,OutputArray mean,OutputArray stddev,InputArray mask=noArray())</span><br></pre></td></tr></table></figure>

<p>src：待求平均值的图像矩阵</p>
<p>mean：图像每个通道的平均值，参数为Mat类型变量</p>
<p>stddev：图像每个通道的标准差，参数为Mat类型变量</p>
<p>mask：用于标记求取哪些区域的平均值和标准差</p>
<p>该函数没有返回值，因为返回的内容在传入的参数中，第二个参数是平均值，第三个参数是标准差</p>
<p><strong>图像的平均值越大，图像整体越亮；图像的标准差越大，图像中明暗的对于越明显</strong></p>
<p>如果对Scalar类型想有更深入的理解，推荐这位博主的博客：<a target="_blank" rel="noopener" href="https://blog.csdn.net/liuweiyuxiang/article/details/76929534">OpenCV学习笔记（3）——Scalar数据类型理解_Lavi的专栏-CSDN博客_scalar</a> </p>
<p>来一个示例程序：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;opencv2\opencv.hpp&gt;</span>  <span class="comment">//加载OpenCV4的头文件</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> cv; <span class="comment">//OpenCV命名空间</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	system(<span class="string">&quot;color F0&quot;</span>);  <span class="comment">//更改界面输出颜色</span></span><br><span class="line">	<span class="keyword">float</span> a[<span class="number">12</span>] = &#123; <span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">10</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">0</span> &#125;;</span><br><span class="line">	Mat img = Mat(<span class="number">3</span>, <span class="number">4</span>, CV_32FC1, a);  <span class="comment">//单通道矩阵，3*4</span></span><br><span class="line">	Mat imgs = Mat(<span class="number">2</span>, <span class="number">3</span>, CV_32FC2, a);  <span class="comment">//多通道矩阵,2*3*2</span></span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;/* 用mean求取图像的平均值 */&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	Scalar myMean;</span><br><span class="line">	myMean = mean(imgs);</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;imgs平均值=&quot;</span> &lt;&lt; myMean &lt;&lt; <span class="built_in">endl</span>;  <span class="comment">//输出格式为[0,0,0,0]分别对应第一，二，三，四通道</span></span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;imgs第一个通道的平均值=&quot;</span> &lt;&lt; myMean[<span class="number">0</span>] &lt;&lt;<span class="string">&quot;		&quot;</span>&lt;&lt; <span class="string">&quot;imgs第二个通道的平均值=&quot;</span> &lt;&lt; myMean[<span class="number">1</span>] &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;/* 用meanStdDev同时求取图像的平均值和标准差 */&quot;</span>&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">	Mat myMeanMat, myStddevMat;</span><br><span class="line">	meanStdDev(img, myMeanMat, myStddevMat);  <span class="comment">//平均值和标准差</span></span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;img平均值=&quot;</span> &lt;&lt; myMeanMat &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;img标准差=&quot;</span> &lt;&lt; myStddevMat &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	meanStdDev(imgs, myMeanMat, myStddevMat);</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;imgs平均值=&quot;</span> &lt;&lt; myMeanMat &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;imgs标准差=&quot;</span> &lt;&lt; myStddevMat &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;  <span class="comment">//程序结束</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="https://img-blog.csdnimg.cn/126c428ecab9450287962e030ea3e685.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="70%">

<p>根据输出结果可以看出，如果不对某一通道进行针对性输出，那么输出的格式就为[0,0,0,0]格式，分别对应第一，二，三，四通道。</p>
<p>感谢阅读！</p>
<p>也欢迎大家关注小白博主，多多鼓励一下！ </p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>杨大熊
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://yang-da-xiong.gitee.io/2022/02/12/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%83%8F%E7%B4%A0%E5%A4%84%E7%90%86/" title="opencv学习笔记之像素处理">https://yang-da-xiong.gitee.io/2022/02/12/opencv学习笔记之像素处理/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/c/" rel="tag"># c++</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/02/10/%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0%E9%A2%9828---%E8%93%9D%E6%A1%A5%E6%9D%AF2021%E7%9C%81%E8%B5%9B%E2%80%9C%E5%8D%A1%E7%89%87%E2%80%9D/" rel="prev" title="算法练习题28---蓝桥杯2021省赛“卡片”">
      <i class="fa fa-chevron-left"></i> 算法练习题28---蓝桥杯2021省赛“卡片”
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/02/12/%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0%E9%A2%9829---%E8%93%9D%E6%A1%A5%E6%9D%AF2021%E7%9C%81%E8%B5%9B%E2%80%9C%E6%97%B6%E9%97%B4%E6%98%BE%E7%A4%BA%E2%80%9D/" rel="next" title="算法练习题29---蓝桥杯2021省赛“时间显示”">
      算法练习题29---蓝桥杯2021省赛“时间显示” <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%A4%9A%E9%80%9A%E9%81%93%E7%9A%84%E5%88%86%E7%A6%BB%E5%90%88%E5%B9%B6"><span class="nav-number">2.</span> <span class="nav-text">一、多通道的分离合并</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%9A%E9%80%9A%E9%81%93%E5%88%86%E7%A6%BB%E5%87%BD%E6%95%B0split%EF%BC%88%EF%BC%89"><span class="nav-number">2.0.0.1.</span> <span class="nav-text">多通道分离函数split（）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%9A%E9%80%9A%E9%81%93%E5%90%88%E5%B9%B6%E5%87%BD%E6%95%B0merge%EF%BC%88%EF%BC%89"><span class="nav-number">2.0.0.2.</span> <span class="nav-text">多通道合并函数merge（）</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%9B%BE%E5%83%8F%E5%83%8F%E7%B4%A0%E7%BB%9F%E8%AE%A1"><span class="nav-number">3.</span> <span class="nav-text">二、图像像素统计</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%A1%E7%AE%97%E5%9B%BE%E5%83%8F%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC%E5%92%8C%E6%A0%87%E5%87%86%E5%B7%AE"><span class="nav-number">3.0.0.1.</span> <span class="nav-text">计算图像的平均值和标准差</span></a></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="杨大熊"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">杨大熊</p>
  <div class="site-description" itemprop="description">各类IT知识分享</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">58</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/Yang-Da-xiong?tab=repositories" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Yang-Da-xiong?tab&#x3D;repositories" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45775703?spm=1000.2115.3001.5343" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45775703?spm&#x3D;1000.2115.3001.5343" rel="noopener" target="_blank"><i class="fab fa-codiepie fa-fw"></i>CSDN</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">杨大熊</span>
</div>

<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>

<div class="theme-info">
<div class="powered-by"></div>
<span class="post-count">博客全站共56.5k字</span>
</div>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"log":false});</script></body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/schemes/clicklove.js"></script>
